package com.apes.autoparts.parts.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lishikun on 2021/5/28 10:43
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EpcSearchLog.CONST_TABLE_NAME)
public class EpcSearchLog extends Model {
    public static final String CONST_TABLE_NAME = "EPC_SEARCH_LOG";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商平台")
    @Comment("唯一码")
    private String id;

    @Column(name = "CPID")
    @Comment("厂牌ID")
    private String CPID;

    @Column(name = "CPMC")
    @Comment("厂牌名称")
    private String CPMC;

    @Column(name = "searchText")
    @Comment("搜索文本")
    private String searchText;


    @Column(name = "source")
    @Comment("来源")
    private String source;

    @Column(name = "vin")
    @Comment("车架号")
    private String vin;

    @Column(name="search_time")
    @Comment("搜索时间")
    private Date searchTime;

    // 厂牌
    @Column(name="CP")
    @Comment("厂牌")
    private String CP;

    // 返回量
    @Transient
    private int count;
    public int getCount(){
        return epcSearchLogItems != null ? epcSearchLogItems.size() : 0;
    }
    /***
     * 日志明细
     */
    @OneToMany(mappedBy = "epcSearchLog", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<EpcSearchLogItem> epcSearchLogItems = new ArrayList<>();

    public List<EpcSearchLogItem> getEpcSearchLogItems() {
        if (epcSearchLogItems == null) return new ArrayList<>();
        for (EpcSearchLogItem item : epcSearchLogItems) {
            item.setEpcSearchLog(this);
        }
        return epcSearchLogItems;
    }

}
